<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class isp_produk extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('isp_produk_model', 'IspProduk');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Produk";
		viewByTemplate('isp_produk/index', $data, 'grid');
	}

	function id_auto(){
		$query = $this->db->query("SELECT id_produk FROM (SELECT * FROM isp_produk ORDER BY id_produk DESC) WHERE rownum <=1");
	 				if ($query->row_array()) {
	 					$getquery = $query->row_array();
	 					$rowcount = $getquery['id_produk'];
	 					$autonum = $rowcount + 1;
	 				} else {
					$autonum = 1;
				}
		$data['kode_id_produk'] = $autonum;	
		echo json_encode($data);
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->IspProduk->GetJSON();
	}


	function getdata2()
	{
		$this->secure_session();
		echo $this->IspProduk->GetJSON2();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		// Validation		
		 if (post("kode_jenis_barang") == '') {
			$error->Add("kode_jenis_barang", "Kode Produk", $tpl_empty);
		} else if (post("bandwidth") == '') {
			$error->Add("bandwidth", "Bandwidth", $tpl_empty);
		} else if (post("harga") == '') {
			$error->Add("harga", "Harga", $tpl_empty);
		}
		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				/*$data	= post_by_key(array('id_produk','kode_jenis_barang','nama_jenis_barang', 'bandwidth', 'harga', 'keterangan'));
				//$data['kode_jenis_barang']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['kode_jenis_barang'])));
				*/
				$data	=  array(
					'id_produk' => $this->input->post('id_produk'),
					'kode_jenis_barang' => $this->input->post('kode_jenis_barang'),
					'nama_jenis_barang' => $this->input->post('nama_jenis_barang'),
					'bandwidth' => $this->input->post('bandwidth'),
					'harga' => $this->input->post('harga'),
					'keterangan' => $this->input->post('keterangan'),
					);

				if (post("mode") == "create")
				{
					//$data['create_date']	= NOW;
					//$data['create_user']	= USERID;


					$this->IspProduk->Add($data);

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.role_id ?? ""));

				}
				else if (post("mode") == "edit")
				{
					//$data['modify_date']	= NOW;
					//$data['modify_user']	= USERID;
					
					$this->IspProduk->Update($data, 
										array('id_produk'	=> $data['id_produk']));

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS002", "edit/" + (data.role_id ?? ""));

				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result		= "";
		$pesan			= "Data Terhapus";
		$id_produk 	= get('id_produk');
		
		if ($id_produk != "")
		{
			// Delete Single
			$arr_rule	= array('id_produk' => $id_produk);
			$ls_result = $this->IspProduk->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('id_produk' => $row['id_produk']);
				$ls_result .= $this->IspProduk->DeleteSingle($arr_rule);
			}
		}
		
		echo $pesan ; 
		echo "
			<script>
				loadPage(gs_path + '/' + controller + '/index');
			</script>
		";
	}
}
?>